<template>
	<view class="checktab relative">
		<view class="flex">
			<u-tabs :list="list1" @change="change" lineColor="#FFB70D" lineHeight="4" style="width: 80%;" :activeStyle="{
            color: '#FFB70D',
            fontWeight: 'bold',
            transform: 'scale(1.1)'
        }" :inactiveStyle="{color: '#606266',transform: 'scale(1)'}" :current='current' scrollable="true">
			</u-tabs>
			<view class="more absolute" @click="moreClick">
				<text>
					查看更多
				</text>
				<image src="../../static/icon/right.png" mode="" class="rightpic"></image>
			</view>
		</view>
		<view class="ly" v-if="isShow=='ly'">
			tips : 领养分为有偿领养和无偿领养
			<card :dataList="datalist" :isDate="false" :isSale="true" @onclickHandler="clickHandler"></card>
		</view>
		<!-- 	<view class="ly" v-if="isShow=='sy'">
			送养
			<card :dataList="datalist" :isDate="false" @onclickHandler="clickHandler"></card>
		</view> -->
		<view class="ly" v-if="isShow=='gm'">
			<card :dataList="goodsList" :isDate="false" :isSale="true" @onclickHandler="clickHandler"></card>
		</view>
	</view>
</template>

<script>
	import card from "../carditem/carditem.vue"
	export default {
		name: "checktab",
		data() {
			return {
				list1: [{
						name: '去认领',
					},
					// {
					// 	name: '去送养',
					// }, 
					{
						name: '去购买'
					}
				],
				current: 0,
				datalist: [],
				goodsList:[]
			};
		},
		methods: {
			change(e) {
				// console.log(e)
				this.current = e.index
			},
			clickHandler(item, index) {
				uni.navigateTo({
					url: "/subpack/petmarketdetail/petmarketdetail?type=" + this.isShow + "&id=" + item._id
				})
			},
			moreClick() {
				uni.navigateTo({
					url: "/subpack/petmarketmore/petmarketmore?type=" + this.isShow
				})
			},
			// 查询捡到
			searchLost() {
				wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'searchLost',
						limit: 2,
					}
				}).then(res => {
					// this.datalist= res.result.data
					this.format(res.result.data)
				})
			},
			// 查询售卖宠物
			searchGoods() {
				wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'getPetGoods',
						limit: 2,
					}
				}).then(res => {
					// this.datalist= res.result.data
					this.format(res.result.data, 1)
				})
			},
			format(arr, lock = 0) {
				for (let i = 0; i < arr.length; i++) {
					let item = arr[i]
					let img = item.swiperList[0]
					if (lock) {
						this.goodsList.push({
							...item,
							img
						})
					} else {
						this.datalist.push({
							...item,
							img
						})
					}
				}
			}
		},
		computed: {
			isShow() {
				if (this.current == 0) {
					if (!this.datalist.length) {
						this.searchLost()
					}
					return 'ly'
				}
				// else if (this.current == 1) {
				// 	return 'sy'
				// }
				else if (this.current == 1) {
					if (!this.goodsList.length) {
						this.searchGoods()
					}
					return 'gm'
				}
			}
		},
		components: {
			card
		},
		mounted() {
			// console.log("mounted:")
			// this.searchLost() // 认领
		}
	}
</script>

<style lang="scss">
	.checktab {
		.more {
			right: 0;
			top: 20upx;

			.rightpic {
				width: 32rpx;
				height: 32rpx;
				padding-top: 10rpx;
				font-size: $uni-font-size-sm;
				color: $uni-text-color-grey;
			}
		}

		.ly {}
	}
</style>